package cn.sz.dzy.provider.service.impl;

import cn.hutool.json.JSONUtil;
import cn.sz.dzy.common.enums.StatusCode;
import cn.sz.dzy.common.pojo.*;
import cn.sz.dzy.common.util.util.DigestUtils;
import cn.sz.dzy.provider.service.ISessionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @program: gta1400
 * @description:
 * @author: DingZhenYun
 * @create: 2021-12-07 15:59
 **/
@Service
@Slf4j
public class SessionServiceImpl implements ISessionService {

    String Digest_realm = "myrealm";
    String qop = "auth";
    String nonce = "dmktZGlnZXN0OjQzNTQyNzI3Nzg";
    String opaque = "dzy";
    String method = "POST";
    String response = "";//加密生成的
    String service_response = "";//加密生成的


    @Override
    public ResponseEntity<String> firstRegister(RegisterRequestObject registerRequestObject) {
        RegisterRequestObject.RegisterObject registerObject = registerRequestObject.getRegisterObject();
        log.info("SessionServiceImpl-registerObject：{}", registerObject);
        //设置响应头
        HttpHeaders responseHeader = new HttpHeaders();
        String WWW = "Digest realm=" + Digest_realm + ",qop=" + qop + ",nonce=" + nonce + ",opaque=" + opaque + ",method=" + method;
        responseHeader.add("WWW-Authenticate", WWW);
        //设置响应体
        ResponseEntity<String> responseEntity = new ResponseEntity("第一次登陆成功", responseHeader, HttpStatus.UNAUTHORIZED);
        log.info("SessionServiceImpl-responseEntity：{}", responseEntity);
        return responseEntity;
    }


    @Override
    public ResponseEntity<String> secondRegister(EncryptionPwVO encryptionPwVO, RegisterRequestObject registerRequestObject) {
        //校验加密结果
        log.info("encryptionPwVO：{}", encryptionPwVO);
        log.info("registerRequestObject：{}", registerRequestObject);
        response=encryptionPwVO.getResponse();
        log.info("实现类参数realm：{}",  Digest_realm);
        log.info("qop：{}",  qop);
        log.info("nonce：{}",  nonce);
        log.info("opaque：{}",  opaque);
        log.info("method：{}",  method);
        log.info("username：{}",  encryptionPwVO.getUsername());
        log.info("password：{}",  encryptionPwVO.getPassword());
        log.info("nc：{}", encryptionPwVO.getNc());
        log.info("cnonce：{}", encryptionPwVO.getCnonce());
        log.info("encryptionPwVO.getUri()：{}", encryptionPwVO.getUri());
        service_response = DigestUtils.getResponse(encryptionPwVO.getUsername(), Digest_realm, encryptionPwVO.getPassword(), nonce, encryptionPwVO.getNc(), encryptionPwVO.getCnonce(), qop, method, encryptionPwVO.getUri());
        log.info("service_response校验结果：{}", service_response);
        log.info("response校验结果：{}", response);
        if(StringUtils.equals(response,service_response)){
            //验证参数一致，注册记录记库，返回200

            //---傻逼了竟然使用这个类
//            ResponseStatusListObject responseStatusListObject=new ResponseStatusListObject();
//            ResponseStatusListObject.ResponseStatus responseStatus=new ResponseStatusListObject.ResponseStatus();
//            responseStatus.setId(registerRequestObject.getRegisterObject().getDeviceID());
//            responseStatus.setLocalTime(DateTimeFormatter.ofPattern("YYYYMMddHHmmss").format(LocalDateTime.now()));
//            responseStatus.setRequestURL(encryptionPwVO.getUri());
//            responseStatus.setStatusCode(String.valueOf(StatusCode.OK.getCode()));
//            responseStatus.setStatusString(StatusCode.OK.getName());
//            List<ResponseStatusListObject.ResponseStatus> list=new ArrayList<ResponseStatusListObject.ResponseStatus>();
//            list.add(responseStatus);
//            ResponseStatusListObject.ResponseStatusList responseStatusList = new ResponseStatusListObject().new ResponseStatusList();
//            responseStatusList.setResponseStatusObject(list);
//            responseStatusListObject.setResponseStatusList(responseStatusList);

            //todo 登记记库

            //--ResponseStatusObjectWrapper
            ResponseStatusObjectWrapper responseStatusObjectWrapper=new ResponseStatusObjectWrapper();
            ResponseStatusObjectWrapper.ResponseStatusObject responseStatusObject = new ResponseStatusObjectWrapper().new ResponseStatusObject();
            responseStatusObject.setId(registerRequestObject.getRegisterObject().getDeviceID());
            responseStatusObject.setLocalTime(DateTimeFormatter.ofPattern("YYYYMMddHHmmss").format(LocalDateTime.now()));
            responseStatusObject.setRequestURL(encryptionPwVO.getUri());
            responseStatusObject.setStatusCode(StatusCode.OK.getCode());
            responseStatusObject.setStatusString(StatusCode.OK.getName());
            responseStatusObjectWrapper.setResponseStatusObject(responseStatusObject);
            ResponseEntity responseEntity = new ResponseEntity(JSONUtil.toJsonStr(responseStatusObjectWrapper), HttpStatus.OK);
            return responseEntity;
        }
        //返回拒绝服务应答
        ResponseEntity<String> responseEntity = new ResponseEntity("校验失败", HttpStatus.FORBIDDEN);
        return responseEntity;
    }

    @Override
    public ResponseEntity<String> firstunregister(UnRegisterRequestObject unRegisterRequestObject) {
        UnRegisterRequestObject.UnRegisterObject unRegisterObject = unRegisterRequestObject.getUnRegisterObject();
        log.info("SessionServiceImpl-registerObject：{}", unRegisterObject);
        //设置响应头
        HttpHeaders responseHeader = new HttpHeaders();
        String WWW = "Digest realm=" + Digest_realm + ",qop=" + qop + ",nonce=" + nonce + ",opaque=" + opaque + ",method=" + method;
        responseHeader.add("WWW-Authenticate", WWW);
        //设置响应体
        ResponseEntity<String> responseEntity = new ResponseEntity("第一次登陆成功", responseHeader, HttpStatus.UNAUTHORIZED);
        log.info("注销：{}", responseEntity);
        return responseEntity;
    }

    @Override
    public ResponseEntity<String> secondunregister(EncryptionPwVO encryptionPwVO, UnRegisterRequestObject unRegisterRequestObject) {
        //校验加密结果
        log.info("encryptionPwVO：{}", encryptionPwVO);
        log.info("registerRequestObject：{}", unRegisterRequestObject);
        response=encryptionPwVO.getResponse();
        log.info("实现类参数realm：{}",  Digest_realm);
        log.info("qop：{}",  qop);
        log.info("nonce：{}",  nonce);
        log.info("opaque：{}",  opaque);
        log.info("method：{}",  method);
        log.info("username：{}",  encryptionPwVO.getUsername());
        log.info("password：{}",  encryptionPwVO.getPassword());
        log.info("nc：{}", encryptionPwVO.getNc());
        log.info("cnonce：{}", encryptionPwVO.getCnonce());
        log.info("encryptionPwVO.getUri()：{}", encryptionPwVO.getUri());
        service_response = DigestUtils.getResponse(encryptionPwVO.getUsername(), Digest_realm, encryptionPwVO.getPassword(), nonce, encryptionPwVO.getNc(), encryptionPwVO.getCnonce(), qop, method, encryptionPwVO.getUri());
        log.info("service_response校验结果：{}", service_response);
        log.info("response校验结果：{}", response);
        if(StringUtils.equals(response,service_response)){
            //todo 登记记库
            //验证参数一致，注册记录记库，返回200
            ResponseStatusObjectWrapper responseStatusObjectWrapper=new ResponseStatusObjectWrapper();
            ResponseStatusObjectWrapper.ResponseStatusObject responseStatusObject = new ResponseStatusObjectWrapper().new ResponseStatusObject();
            responseStatusObject.setId(unRegisterRequestObject.getUnRegisterObject().getDeviceID());
            responseStatusObject.setLocalTime(DateTimeFormatter.ofPattern("YYYYMMddHHmmss").format(LocalDateTime.now()));
            responseStatusObject.setRequestURL(encryptionPwVO.getUri());
            responseStatusObject.setStatusCode(StatusCode.OK.getCode());
            responseStatusObject.setStatusString(StatusCode.OK.getName());
            responseStatusObjectWrapper.setResponseStatusObject(responseStatusObject);
            ResponseEntity responseEntity = new ResponseEntity(JSONUtil.toJsonStr(responseStatusObjectWrapper), HttpStatus.OK);
            return responseEntity;
        }
        //返回拒绝服务应答
        ResponseEntity<String> responseEntity = new ResponseEntity("校验失败", HttpStatus.FORBIDDEN);
        return responseEntity;
    }

    @Override
    public ResponseEntity<String> keepalive(KeepaliveRequestObject keepaliveRequestObject) {
        KeepaliveRequestObject.KeepaliveObject keepaliveObject=keepaliveRequestObject.getKeepaliveObject();
        //todo 记录心跳
        ResponseEntity<String> responseEntity = new ResponseEntity("成功",HttpStatus.OK);
        return responseEntity;
    }

    @Override
    public ResponseEntity<String> time(SystemTimeObject systemTimeObject) {
        SystemTimeObject.SystemTime systemTime = systemTimeObject.getSystemTime();
        String yyyymMddHHmmss = DateTimeFormatter.ofPattern("YYYYMMddHHmmss").format(LocalDateTime.now());
        systemTime.setLocalTime(yyyymMddHHmmss);
        systemTime.setTimeMode("1");//1网络2手动
        ZonedDateTime zbj = ZonedDateTime.now(); //默认时区
        systemTime.setTimeZone(zbj.toString());
        ResponseEntity<String> responseEntity = new ResponseEntity(JSONUtil.toJsonStr(systemTimeObject), HttpStatus.OK);
        return responseEntity;
    }


}
